-----------------------------------------

set nocount on
declare @n int
create table #lr (x real not null, y real not null)
--insert sample data
insert into #lr
Select L1.ISTAT.ANNO_2016 ,L1.ISTAT.ANNO_2017
from L1.ISTAT
where Indicatore in ('Tasso di disoccupazione')
select @n=count(*) from #lr --this is just for convenience, you can substitute count(*) for @n in the code

--meat of the code begins here
select
((@n * sum(x*y)) - (sum(x)*sum(y)))/
((@n * sum(Power(x,2)))-Power(Sum(x),2)) AS Slope,
avg(y) - ((@n * sum(x*y)) - (sum(x)*sum(y)))/
((@n * sum(Power(x,2)))-Power(Sum(x),2)) * avg(x) as Intercept
from #lr
drop table #lr
----------------------------------------------------------------

--------------------------------REGRESSION  2016-1017!!!!---------------------------
---- x= Anno_2016
---- y= Anno_2017
  -----  Where Indicatore in ('Rete ferroviaria in esercizio')
  -----  Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
  -----  Where Indicatore in ('Tasso di disoccupazione')
  -----  Where Indicatore in ('Pil pro capite')
------To calculate the intercept, we wrap the above query in another subquery and reuse the calculation:


select slope,
       y_bar_max - x_bar_max * slope as intercept
from (------To calculate the slope
       select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope,
              max(x_bar)     as x_bar_max,
              max(y_bar)     as y_bar_max
       from ( ---We can compute the averages  and  for all the rows in the table:
              select Anno_2016        as x,
                     avg(Anno_2016) over() as x_bar,
                     ANNO_2017        as y,
                     avg(Anno_2017) over() as y_bar
              from l1.ISTAT
			  Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
              -- Where Indicatore in ('Rete ferroviaria in esercizio')
              -- Where Indicatore in ('Tasso di disoccupazione')
              -- Where Indicatore in ('Pil pro capite')
         ) s
     )t


--------------------------------------------------------------
-----y= A+ xB----------A=SLOPE----------B=INTERCEPTION--------
--------------------------------------------------------------
select x, y, (1.0101436306917146) * x +(-0.5105001493632404) as y_fit
from   (select ANNO_2016        as x,
                ANNO_2017        as y
                    from l1.ISTAT
                    Where Indicatore in ('Tasso di disoccupazione')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (1.011618597166806) * x +(-0.22566385333946926) as y_fit
from   (select ANNO_2016        as x,
                ANNO_2017        as y
                    from l1.ISTAT
                    Where Indicatore in ('Rete ferroviaria in esercizio')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (1.013702090860298) * x +(-3.462327889414155) as y_fit
from   (select ANNO_2016        as x,
                ANNO_2017        as y
                    from l1.ISTAT
                    Where Indicatore in ('Pil pro capite')
               ) s
--------------------------------------------------------------
--------------------------------------------------------------
select x, y, (0.952308013119217) * x +(123.27092518685504) as y_fit
from   (select ANNO_2016        as x,
                ANNO_2017        as y
                    from l1.ISTAT
                    Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
               ) s
--------------------------------------------------------------
----------------------------------------------------------------

--------------------------------REGRESSION  2015-1016!!!!---------------------------
---- x= Anno_2015
---- y= Anno_2016
  -----  Where Indicatore in ('Rete ferroviaria in esercizio')
  -----  Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
  -----  Where Indicatore in ('Tasso di disoccupazione')
  -----  Where Indicatore in ('Pil pro capite')
------To calculate the intercept, we wrap the above query in another subquery and reuse the calculation:
select slope,
       y_bar_max - x_bar_max * slope as intercept
from (------To calculate the slope
       select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope,
              max(x_bar)     as x_bar_max,
              max(y_bar)     as y_bar_max
       from ( ---We can compute the averages  and  for all the rows in the table:
              select Anno_2015        as x,
                     avg(Anno_2015) over() as x_bar,
                     ANNO_2016        as y,
                     avg(Anno_2016) over() as y_bar
              from l1.ISTAT
              Where Indicatore in ('Rete ferroviaria in esercizio')
             --Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
               -- Where Indicatore in ('Tasso di disoccupazione')
              --Where Indicatore in ('Pil pro capite')
         ) s
     )t


--------------------------------------------------------------
-----y= A+ xB----------A=SLOPE----------B=INTERCEPTION--------
--------------------------------------------------------------
select x, y, (1.024255035266965) * x +(-0.4945336137135641) as y_fit
from   (select ANNO_2015        as x,
                ANNO_2016        as y
                    from l1.ISTAT
                    Where Indicatore in ('Tasso di disoccupazione')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (0.9986494036452478) * x +(0.07383002724951382) as y_fit
from   (select ANNO_2015        as x,
                ANNO_2016        as y
                    from l1.ISTAT
                    Where Indicatore in ('Rete ferroviaria in esercizio')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (1.0075620812301112) * x +(-4.696950954506974) as y_fit
from   (select ANNO_2015        as x,
                ANNO_2016        as y
                    from l1.ISTAT
                    Where Indicatore in ('Pil pro capite')
               ) s
--------------------------------------------------------------
--------------------------------------------------------------
select x, y, (1.0180451098412586) * x +(-22.701664643987897) as y_fit
from   (select ANNO_2015        as x,
                ANNO_2016        as y
                    from l1.ISTAT
                    Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
               ) s
--------------------------------------------------------------
----------------------------------------------------------------
--------------------------------REGRESSION 2014-1015!!---------------------------
---- x= Anno_2014
---- y= Anno_2015
  -----  Where Indicatore in ('Rete ferroviaria in esercizio')
  -----  Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
  -----  Where Indicatore in ('Tasso di disoccupazione')
  -----  Where Indicatore in ('Pil pro capite')
------To calculate the intercept, we wrap the above query in another subquery and reuse the calculation:


select slope,
       y_bar_max - x_bar_max * slope as intercept
from (------To calculate the slope
       select sum((x - x_bar) * (y - y_bar)) / sum((x - x_bar) * (x - x_bar)) as slope,
              max(x_bar)     as x_bar_max,
              max(y_bar)     as y_bar_max
       from ( ---We can compute the averages  and  for all the rows in the table:
              select Anno_2014        as x,
                     avg(Anno_2014) over() as x_bar,
                     ANNO_2015        as y,
                     avg(Anno_2015) over() as y_bar
              from l1.ISTAT
			  --Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
              -- Where Indicatore in ('Rete ferroviaria in esercizio')
              -- Where Indicatore in ('Tasso di disoccupazione')
              Where Indicatore in ('Pil pro capite')
         ) s
     )t


--------------------------------------------------------------
-----y= A+ xB----------A=SLOPE----------B=INTERCEPTION--------
--------------------------------------------------------------
select x, y, (0.9439393745291607) * x +(-0.02413956544337026) as y_fit
from   (select ANNO_2014        as x,
                ANNO_2015        as y
                    from l1.ISTAT
                    Where Indicatore in ('Tasso di disoccupazione')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (1.0057227107577271) * x +(-0.058823442683369365) as y_fit
from   (select ANNO_2014        as x,
                ANNO_2015        as y
                    from l1.ISTAT
                    Where Indicatore in ('Rete ferroviaria in esercizio')
               ) s
--------------------------------------------------------------
  ------------------------------------------------------------
select x, y, (1.0077520306821661) * x +(46.96558583348815) as y_fit
from   (select ANNO_2014        as x,
                ANNO_2015        as y
                    from l1.ISTAT
                    Where Indicatore in ('Pil pro capite')
               ) s
--------------------------------------------------------------
--------------------------------------------------------------
select x, y, (0.9809472662480755) * x +(31.17963773777251) as y_fit
from   (select ANNO_2016        as x,
                ANNO_2017        as y
                    from l1.ISTAT
                    Where Indicatore in ('Spesa media mensile familiare per beni e servizi non alimentari')
               ) s
--------------------------------------------------------------
----------------------------------------------------------------